<?php
namespace App\Http\Controllers;


use App\Student;

class StudentController extends Controller{
	//查询
	public function orm1(){

	    //all()
        $student = Student::all();
        //find()
        $student = Student::find(2);

        //findOrFail()
       // $student = Student::findOrFail(100);

        //用查询构造器方法
        $student = Student::get();
       // $student = Student::where('id','>','10')->orderBy('time','desc')->first();
       // dd($student);

    //    Student::chunk(2,function($student){
     //       var_dump($student);
      //  });

        //聚合函数
        //$num = Student::count();
       $max =  Student::where('id','>','10')->max('time');
        var_dump($max);
    }

    //新增
    public function orm2(){

	    //使用模型新增数据
        /*
        $student =new Student();
        $student->name='sean';
        $student->pass="111111";
        $bool = $student->save();

        dd($bool);*/

       // $student = Student::find('1');
       // echo date('Y-m-d H:i:s',$student->time);

        // 使用模型的Create方法新增数据
       // $student = Student::create(
       //     ['name'=>'imooc','pass'=>'18']
        //);
       // dd($student);

        //firstOrCreate()使用指定的字段／值对，来尝试寻找数据库中的记录。如果在数据库中找不到模型，则会使用指定的属性来添加一条记录。
      // $student = Student::firstOrCreate(
      //      ['name' => 'imoocs'],['pass' => '123']
       // );
        //dd($student);
        //echo $student->name;

        //firstOrNew()
        $student = Student::firstOrNew(
            ['name' => 'imoocsss'],['pass' => '123']
        );
        $bool =  $student->save();

        dd($bool);
    }

    //修改
    public function orm3(){
        //通过模型更新数据
      /* $student = Student::find(36);
       //echo $student;
       $student->name = 'kitty';
       $bool = $student->save();
       var_dump($bool);*/

      //批量更新
       $num = Student::where('id','>',34)->update(
           ['pass'=>'12123fsdf']
        );
       var_dump($num);
    }

    //删除
    public function orm4(){
        //通过模型删除
     //  $student = Student::find(36);

      // $bool = $student->delete();
     //  var_dump($bool);

        //通过主键删除
     //   $num = Student::destroy(33,34);
      //  var_dump($num);

       $num = Student::where('id','>',31)->delete();
        var_dump($num);
		
    }


}

?>